<?php
/**
 * Portal Cache
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalCache.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 * @package Pradoportal.Common
 *
 */

class PortalCache
{
	/* Cache object Id.
	 *
	 * @var string cacheObjectId
	 * @access private */
	private $cacheObjectId = "";

	/* Cache object timeout.
	 *
	 * @var integer [$timeout] timeout
	 * @access private */
	private $timeout;

	/* Cache object timeout.
	 *
	 * @var integer [$stdtimeout] standard timeout
	 * @access private */
	private $stdtimeout	= 216000;

	/* Constructor
	 *
	 * Sets cache object path
	 *
	 * @param string cacheObjectId
	 * @param integer timeout
	 * @return boolean
	 * @access public */
	public function __construct($cacheObjectId, $timeout = null)
	{
		$this->cacheObjectId	= $cacheObjectId;
		$this->timeout			= ($timeout !== null ? $timeout : $this->stdtimeout);
		return true;
	}

	/* Create cache object.
	 *
	 * Creates a cache object with passed contents
	 *
	 * @param string contents
	 * @return boolean
	 * @access private */
	public function createCacheObject ($contents)
	{
		if(strlen($contents))
			return file_put_contents ($this->cacheObjectId,$contents);
		else
			return true;
	}

	/* Updates cache object.
	 *
	 * Updates a cache object with passed contents
	 *
	 * @param string contents
	 * @return string contents
	 * @access public */
	public function updateCacheObject ($contents)
	{
		/* check to see if object exists */
		if ($this->cacheObjectExists())
		{
			/* check if its locked */
			if (!$this->cacheObjectLocked())
			{
				/* if object exists lock it */
				$this->lockCacheObject();

				/* then create new one */
				$this->createCacheObject($contents);
				$this->unlockCacheObject();
				return $contents;
			}
			else
			{
				return $this->loadLockedObject();
			}
		}
		else
		{
			$this->createCacheObject($contents);
			return $contents;
		}
	}

	/* Load cache object.
	 *
	 * Loads cache object and returns its contents
	 *
	 * @return string contents
	 * @access public */
	public function loadCacheObject()
	{
		if (!$this->cacheObjectLocked())
		{
			return $this->cacheObjectContents($this->cacheObjectId);
		}
		else
		{
			return $this->loadLockedObject();
		}
	}

	/* Delete cache object.
	 *
	 * Deletes a cache object
	 *
	 * @return boolean
	 * @access private */
	public function deleteCacheObject ()
	{
		if (!$this->cacheObjectLocked() && is_file ($this->cacheObjectId))
		{
			if (unlink ($this->cacheObjectId))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}

	/* Lock cache object.
	 *
	 * Locks a cache object for updating
	 *
	 * @return boolean
	 * @access private */
	public function lockCacheObject ()
	{
		if (copy ($this->cacheObjectId, $this->cacheObjectId.'.lock'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/* Unlock cache object.
	 *
	 * Unlocks a cache object for updating
	 *
	 * @return boolean
	 * @access private */
	public function unlockCacheObject ()
	{
		if (unlink ($this->cacheObjectId.'.lock'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/* Check cache object.
	 *
	 * Checks to see if the cache object exists or not
	 *
	 * @return boolean
	 * @access private */
	public function cacheObjectExists()
	{
		if(file_exists($this->cacheObjectId))
		{
			if (is_file ($this->cacheObjectId))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		return false;
	}

	/* Determines if cache object is locked.
	 *
	 * Checks to see if the cache object is locked or not
	 *
	 * @return boolean
	 * @access private */
	public function cacheObjectLocked ()
	{
		if (is_file ($this->cacheObjectId.'.lock'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/* Cached object has timed out.
	 *
	 * Checks to see if the cache object has expired or not
	 *
	 * @return boolean
	 * @access public */
	public function cacheObjectTimedOut ()
	{
		if ($this->cacheObjectExists())
		{
			if (time () - filemtime ($this->cacheObjectId) > $this->timeout)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}

	/* Load locked cache object.
	 *
	 * Loads a locked cache object and returns its contents
	 *
	 * @return string
	 * @access public */
	public function loadLockedObject()
	{
		return $this->cacheObjectContents($this->cacheObjectId.'.lock');
	}

	/* Cache object contents.
	 *
	 * Loads a file and returns its contents
	 *
	 * @param string filename
	 * @return string
	 * @access private */
	public function cacheObjectContents ($filename)
	{
		return file_get_contents ($filename);
	}
}
?>